package com.kc.business.service.impl;

import com.kc.business.domain.BizRole;
import com.kc.business.domain.BizUserRole;
import com.kc.business.mapper.BizRoleMapper;
import com.kc.business.mapper.BizUserRoleMapper;
import com.kc.business.service.IBizRoleMenuService;
import com.kc.business.service.IBizRoleService;
import com.kc.common.constant.Constants;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * FileName: BizRoleServiceImpl
 * Author:   TP
 * Date:     2020-10-30 20:22
 * Description:
 */
@Service
public class BizRoleServiceImpl implements IBizRoleService {

    @Autowired
    private BizRoleMapper roleMapper;

    @Autowired
    private IBizRoleMenuService roleMenuService;

    @Autowired
    private BizUserRoleMapper userRoleMapper;

    @Override
    public List<BizRole> getList(BizRole bizRole) {
        return roleMapper.getList(bizRole);
    }

    @Override
    @Transactional
    public AjaxResult add(BizRole role, String username) {
        if (role != null) {
            // 查重
            BizRole existRole1 = roleMapper.getActiveByRoleName(role.getRoleName());
            if (existRole1 != null) {
                return AjaxResult.error("操作失败，已存在此名称的角色，请勿重复创建");
            }
            BizRole existRole2 = roleMapper.getActiveByRoleKey(role.getRoleKey());
            if (existRole2 != null) {
                return AjaxResult.error("操作失败，已存在此标识的角色，请勿重复创建");
            }
            if (roleMapper.save(role) > 0) {
                roleMenuService.saveBatch(role.getId(), role.getMenuIds(), username);
                return AjaxResult.success();
            }
            return AjaxResult.error("操作失败，保存数据失败");
        }
        return AjaxResult.error("操作失败，请指定角色相关信息");
    }

    @Override
    @Transactional
    public AjaxResult update(BizRole role, String username) {
        if (role != null && role.getId() > 0) {
            // 存在性校验
            BizRole existRole = roleMapper.selectByPrimaryKey(role.getId());
            if (existRole == null || Constants.DELETE.equals(existRole.getIsDeleted())) {
                return AjaxResult.error("操作失败，查无此角色");
            }
            // 只更新状态和修改人、修改时间
            BizRole toUpdateRole = new BizRole();
            toUpdateRole.setUpdateTime(new Date());
            toUpdateRole.setUpdateBy(username);
            toUpdateRole.setId(role.getId());
            toUpdateRole.setStatus(role.getStatus());
            if (roleMapper.updateByPrimaryKeySelective(toUpdateRole) > 0) {
                roleMenuService.deleteByRoleId(role.getId());
                roleMenuService.saveBatch(role.getId(), role.getMenuIds(), username);
                return AjaxResult.success();
            }
            return AjaxResult.error("操作失败，保存数据失败");
        }
        return AjaxResult.error("操作失败，请指定角色相关信息");
    }

    @Override
    @Transactional
    public AjaxResult deleteById(Integer roleId, String username) {
        if (roleId != null && roleId > 0) {
            // 获取角色信息
            BizRole bizRole = roleMapper.selectByPrimaryKey(roleId);
            if (bizRole != null) {
                if (Constants.DELETE.equals(bizRole.getIsDeleted())) {
                    return AjaxResult.success();
                }
                // 判断角色是否已经被使用
                BizUserRole userRole = new BizUserRole();
                userRole.setRoleId(roleId);
                List<BizUserRole> bizUserRole = userRoleMapper.select(userRole);
                if (CollectionUtils.isNotEmpty(bizUserRole)) {
                    return AjaxResult.error("操作失败，该角色正在使用中，无法删除");
                }
                if (roleMapper.deleteById(roleId, username) > 0) {
                    userRoleMapper.deleteByRoleId(roleId);
                    roleMenuService.deleteByRoleId(roleId);
                    return AjaxResult.success();
                }
                return AjaxResult.error("操作失败，保存数据失败");
            }
            return AjaxResult.error("操作失败，查无此角色");
        }
        return AjaxResult.error("操作失败，请指定要删除的角色");
    }

    @Override
    public BizRole getByKey(String key) {
        if (StringUtils.isNotEmpty(key)) {
            return roleMapper.getActiveByRoleKey(key);
        }
        return null;
    }
}
